package com.yihuier.yihuier.Mapper.MeetingManager;

import org.apache.ibatis.annotations.*;

import java.util.List;
import java.util.Map;

@Mapper
public interface MeetingUserMapper {

    @Insert("INSERT INTO bz_meeting_user(user_id,meeting_id) VALUES(#{user_id},#{meeting_id});")
    int addOneRecordOfMeetingUserWithOutRole(@Param("user_id")int user_id,@Param("meeting_id")int meeting_id);

    @Insert("INSERT INTO bz_meeting_user(user_id,meeting_id,role) VALUES(#{user_id},#{meeing_id},#{role});")
    int addOneRecordOfMeetingUserWithRole(@Param("user_id")int user_id,@Param("meeting_id")int meeting_id,@Param("role")int role);

    /**
     * 获取与我有关的会议
     * */
    @Select("SELECT user_id,meeting_id,role,status FROM bz_meeting_user WHRER user_id = #{user_id};")
    List<Map<String,Object>> selectRelateToMeMeetings(@Param("user_id")int user_id);

    @Select("SELECT user_id FROM `bz_meeting_user` WHERE meeting_id = #{meeting_id};")
    List<Integer> selectMeetingUserIds(@Param("meeting_id") int meeting_id);

    @Select("SELECT meeting_id FROM bz_meeting_user WHERE user_id = #{user_id};")
    int[] selectMeetingsOfUserHave(@Param("user_id")int user_id);

    /**
     * 获取参加会议的用户id数组
     * */
    @Select("SELECT user_id FROM bz_meeting_user WHERE meeting_id = #{meeting_id} AND status = 1;")
    int[] selectAttendUsers(@Param("meeting_id")Long meeting_id);

    @Insert("INSERT INTO bz_meeting_user(user_id,meeting_id,role) VALUES(#{user_id},#{meeting_id},2);")
    int insertUserApplyToAttendMeeting(@Param("user_id")int user_id,@Param("meeting_id")int meeting_id);

    @Select("SELECT COUNT(user_id) FROM bz_meeting_user WHERE user_id = #{user_id} AND meeting_id = #{meeting_id};")
    int selectThisUserIfJoinMeeting(@Param("user_id")int user_id,@Param("meeting_id")int meeting_id);

    @Update("UPDATE bz_meeting_user SET role = #{status} WHERE user_id = #{user_id} AND meeting_id = #{meeting_id};")
    int updateUserRequest(@Param("user_id") int user_id,@Param("meeting_id") int meeting_id,@Param("status") int status);

    @Select("<script>" +
            " SELECT user_id,meeting_id" +
            " FROM" +
            " bz_meeting_user" +
            " WHERE" +
            " role = 2 AND status = 1 AND" +
            " meeting_id IN" +
            " <foreach item='item' index='index' collection='meeting_ids' open='(' separator=',' close=')'>" +
            " #{item}" +
            " </foreach>" +
            " </script> ")
    List<Map<String,Integer>> selectMeetingApplyUsers(@Param("meeting_ids") int[] meeting_ids);
}
